package com.nariano.server.services.statistics.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.Assert;
import com.nariano.server.domain.entities.PopulationType;
import com.nariano.server.domain.entities.User;
import com.nariano.server.domain.utils.PopulationTypeList;
import com.nariano.server.services.cityservice.CityService;
import com.nariano.server.services.mapservice.MapService;
import com.nariano.server.services.statistics.PopulationDistribution;
import com.nariano.server.services.userservice.UserService;

/**
 * 
 */
public class PopulationDistributionContainer {
	
	transient Logger logger = Logger.getLogger(PopulationDistributionContainer.class.getCanonicalName());
	
	@Autowired
    @Qualifier(value="populationTypeList")
    private PopulationTypeList typeList;
	
	@Autowired
    @Qualifier(value="userService")
    private UserService userService;
	
	@Autowired
    @Qualifier(value="mapService")
    private MapService mapService;
	
	private Map<Integer, PopulationDistribution> popdistribution = new HashMap<Integer, PopulationDistribution>();
	
	/**
	 * Initializes the population distribution for all players
	 * @param popdistribution
	 */
	public void init() {
		List<User> users = userService.getAllUsers();
		for(User user : users) {
			PopulationDistribution distr = new PopulationDistribution();
			popdistribution.put(user.getId(), distr );
			for (PopulationType type : PopulationType.values()) {
				distr.addPopulation(type, mapService.getPopulationCount(user.getId(), type));
			}
		}
	}

	public void addUser(Integer user) {
		if (false == popdistribution.containsKey(user)) {
			popdistribution.put(user, new PopulationDistribution());
		}
	}

	public void addPopulation(Integer user, PopulationType type, int count) {
		popdistribution.get(user).addPopulation(type, count);
	}

	public PopulationDistribution get(Integer id) {
		return popdistribution.get(id);
	}

}
